
export function removeNullProps(obj){
    if(obj !== null){
        if( typeof(obj) === 'undefined') return null;
        if(obj instanceof Date){
            return obj;
        }

        var cloneData = JSON.parse(JSON.stringify(obj));

        if(Array.isArray(obj)){
            for(var iFor = 0; iFor< obj.length; iFor ++){
                var o = obj[iFor];
                const r = removeNullProps(o);
                if(r === null){
                    cloneData.slice(iFor,1);
                }
            }
            if(cloneData !== null && cloneData.length){
                //删完成之后还有值
                return cloneData;
            }else{
                return null;
            }
        }else if(typeof(obj) =='object'){
            for(var key in obj){
                var r = null;

                if(obj[key] !== null){
                    if(obj[key] instanceof Date){
                        r = parseTime(obj[key]);
                        cloneData[key] = r;
                    }else{
                        r = removeNullProps(obj[key]);
                    }
                }
                if(r === null){
                    delete cloneData[key];
                }
            }
            if(cloneData !==null && Object.getOwnPropertyNames(cloneData).length){
                return cloneData;
            }else{
                return null;
            }
        }else{
            return cloneData;
        }
    }
    return null;
}

export function randomStr(sLen){
    var text=['23456789abcdefghijkmnpqrstuvwxyz23456789'];
    var rand = function(min, max){
        return Math.floor(Math.max(min, Math.random() * (max+1)));
    }
    var len = sLen;//rand(8, 16); // 长度为8-16
    var pw = '';
    for(var i=0; i<len; ++i) {
        var strpos = rand(0, text.length - 2 );
        var chrpos = rand(0, text[strpos].length - 1);
        pw += text[strpos].charAt(chrpos);
    }
    return pw;
}


export function parseParam (param, prefix) {
    var paramStr = "";
    if(param == null || typeof(param) == 'undefined'){

    }else if (typeof(param) == 'string' || param instanceof String
        || typeof(param) == 'number' || param instanceof Number
        || typeof(param) == 'boolean' || param instanceof Boolean) {
        paramStr += "&" + encodeURIComponent(prefix) + "=" + encodeURIComponent(param);
    } else if(param instanceof Date){
        paramStr += "&" + encodeURIComponent(prefix) + "=" +  encodeURIComponent(parseTime(param));
    } else {
        for(var key in param) {
            const p = prefix == null ? key : prefix + (param instanceof Array ? "[" + key + "]" : "." + key);
            const cp = parseParam(param[key], p);
            if(cp) {
                paramStr += '&' + cp;
            }
        }
    }
    return paramStr.substr(1);
}

